package tech.aistar.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tech.aistar.pojo.SongList;

import tech.aistar.pojo.SongSinger;
import tech.aistar.pojo.entity.SingerList;
import tech.aistar.service.SingerListService;
import tech.aistar.service.SongListService;
import tech.aistar.vo.Result;
import tech.aistar.vo.ResultVo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("gedanzhuye")
public class GedanzhuyeController {
    @Autowired
    private SongListService songListService;
    @Autowired
    private SingerListService singerListService;

    private SongSinger songSinger;


    //    @GetMapping("gequ/{id}")
//    public ResultVo setSongListService(@PathVariable("id")String id){
//            List<SongList> selectbyid=songListService.findAlllist(Integer.valueOf(id));
//            int count=selectbyid.size();
//            return new ResultVo(0,"查询成功",count,selectbyid);
//    }
    @GetMapping("gequ")
    public void setSongListService(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");//设置相应的文本类型
        resp.setContentType("text/html;charset=utf-8");//设置响应类型,并防止中文乱码
        PrintWriter out = resp.getWriter();
        String id = req.getParameter("id");

        String pageNum=req.getParameter("page");
        String limitStr=req.getParameter("limit");
        pageNum = pageNum == null?"1":pageNum;

        System.out.println(limitStr);

//        String singername = singerListService.getbyid(Integer.valueOf(id));
        List<SongList> selectbyid = songListService.findAlllist(Integer.valueOf(id));
//        PageInfo<SongList> selectbyid = songListService.findBypicture(Integer.valueOf(id),Integer.valueOf(pageNum),Integer.valueOf(limitStr));
//        List<SongSinger> singers = null;
//        int count1 = selectbyid.getSize();
        int count1=selectbyid.size();
//        for (int i = 0; i < count1; i++) {
//            songSinger.setId(selectbyid.get(i).getId());
//            songSinger.setSongTitle(selectbyid.get(i).getSongTitle());
//            songSinger.setSingername(singername);
//            songSinger.setClicks(selectbyid.get(i).getClicks());
//            singers.add(i, songSinger);
//        }
//        singers.forEach(e -> System.out.println(e));
        String count = "" + count1;
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", 0);
        result.put("msg", "查询成功");
        result.put("count", count);
        result.put("data", selectbyid);
        System.out.println(result);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        resp.getWriter().print(json.toString());  //传递json给后端
        System.out.println(result);

    }
    @GetMapping("gequ1")
    public void setSongListService1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");//设置相应的文本类型
        resp.setContentType("text/html;charset=utf-8");//设置响应类型,并防止中文乱码
        PrintWriter out = resp.getWriter();
        String id = req.getParameter("id");

        String pageNum=req.getParameter("page");
        String limitStr=req.getParameter("limit");
        pageNum = pageNum == null?"1":pageNum;

        System.out.println(limitStr);

//        String singername = singerListService.getbyid(Integer.valueOf(id));
        List<SongList> selectbyid = songListService.findAlllist1(Integer.valueOf(id));
//        PageInfo<SongList> selectbyid = songListService.findBypicture(Integer.valueOf(id),Integer.valueOf(pageNum),Integer.valueOf(limitStr));
//        List<SongSinger> singers = null;
//        int count1 = selectbyid.getSize();
        int count1=selectbyid.size();
//        for (int i = 0; i < count1; i++) {
//            songSinger.setId(selectbyid.get(i).getId());
//            songSinger.setSongTitle(selectbyid.get(i).getSongTitle());
//            songSinger.setSingername(singername);
//            songSinger.setClicks(selectbyid.get(i).getClicks());
//            singers.add(i, songSinger);
//        }
//        singers.forEach(e -> System.out.println(e));
        String count = "" + count1;
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", 0);
        result.put("msg", "查询成功");
        result.put("count", count);
        result.put("data", selectbyid);
        System.out.println(result);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        resp.getWriter().print(json.toString());  //传递json给后端
        System.out.println(result);

    }
}
